package com.example.mediamoudle.crop;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.android.baselibrary.utils.ScreenUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.example.mediamoudle.R;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
public  class ThumbAdapter extends RecyclerView.Adapter<ThumbAdapter.ViewHolder> {
    private static final int TYPE_HEAD_FOOT = 0;
    private static final int TYPE_THUMB = 1;
    private List<VideoInfo> list = new ArrayList<>();

    public ThumbAdapter() {

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ImageView view = new ImageView(parent.getContext());
        view.setScaleType(ImageView.ScaleType.FIT_XY);
        view.setLayoutParams(new RecyclerView.LayoutParams(dp2px(50,parent.getContext()),dp2px(50,parent.getContext())));
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        VideoInfo viewType = list.get(position);
        if (viewType.type == TYPE_THUMB) {
            holder.itemView.getLayoutParams().width =  dp2px(50,holder.itemView.getContext());
            Glide.with(holder.itemView.getContext())
                    .asBitmap()
                    .frame(viewType.duration)
                    .load(viewType.videoPath)
                    .into(new CustomTarget<Bitmap>(){
                        @Override
                        public void onResourceReady(@NonNull @NotNull Bitmap resource, @Nullable @org.jetbrains.annotations.Nullable Transition<? super Bitmap> transition) {
                            holder.mIv.setImageBitmap(resource);
                        }

                        @Override
                        public void onLoadCleared(@Nullable @org.jetbrains.annotations.Nullable Drawable placeholder) {

                        }
                    });
        }else {
            holder.itemView.getLayoutParams().width = ScreenUtils.getWidth(holder.itemView.getContext()) / 2;
            Glide.with(holder.itemView.getContext())
                    .load("")
                    .into((ImageView) holder.itemView);
        }
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    public void updateList(@NotNull List<VideoInfo> list) {
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public void updateItem(int position, @NotNull VideoInfo outfile) {
        this.list.set(position, outfile);
        notifyItemChanged(position);
    }

    @Override
    public int getItemViewType(int position) {
        return list.get(position).type;
    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        private  ImageView mIv;

        public ViewHolder(@NonNull ImageView view) {
            super(view);
            this.mIv = view;
        }
    }

    private int dp2px(int dp,Context context) {
        return (int) (dp * context.getResources().getDisplayMetrics().density);
    }
}
